package chenji.watcher.core;

import java.io.IOException;

import org.bukkit.Location;

import chenji.watcher.Main;

public class ConfigManager extends KConfiguration {

	public ConfigManager() throws ConfigException {
		super("data", false, "成功创建数据文件", "无法创建数据文件");
		this.loadConfig();
	}

	@Override
	public void loadConfig() {
		for (String s : this.getConfig().getKeys(false)) {
			WatchGroupData wgd = this.getWatchGroupData(s);
			if (wgd != null) {
				Main.getWatchGroupManager().setWatchGroupData(wgd);
			}
		}
	}

	public WatchGroupData getWatchGroupData(String Name) {
		if (!this.getConfig().contains(Name)) {
			return null;
		}
		String startTitle = this.getConfig().getString(Name + ".StartTitle");
		String endTitle = this.getConfig().getString(Name + ".EndTitle");
		int startTime = this.getConfig().getInt(Name + ".StartTime");
		int endTime = this.getConfig().getInt(Name + ".EndTime");
		boolean canCancel = this.getConfig().getBoolean(Name + ".back");
		WatchGroupData wgd = new WatchGroupData(startTitle, startTime, endTitle, endTime, canCancel, Name);
		if (this.getConfig().contains(Name + ".Teleporters")) {
			for (String Path : this.getConfig().getConfigurationSection(Name + ".Teleporters").getKeys(false)) {
				String Path1 = Name + ".Teleporters." + Path;
				WatchGroupTeleporter w = this.getWatchGroupTeleporter(Path1);
				if (w != null) {
					wgd.registerTeleporter(w);
				}
			}
		}
		return wgd;

	}

	public WatchGroupTeleporter getWatchGroupTeleporter(String Path) {
		String Title = this.getConfig().getString(Path + ".Title");
		Location loc = (Location) this.getConfig().get(Path + ".loc");
		String Name = this.getConfig().getString(Path + ".Name");
		int waitTime = this.getConfig().getInt(Path + ".WaitTime");
		if (Title != null && loc != null && Name != null) {
			if (waitTime <= 0) {
				Main.getLog().warning("加载" + Path + "时发生错误，等待时间不能小于等于0");
				return null;
			}
			return new WatchGroupTeleporter(Name, Title, waitTime, loc);
		} else {
			Main.getLog().warning("加载" + Path + "时发生错误，位置或字幕不能为空");
			return null;
		}

	}

	public void setWatchGroupData(WatchGroupData data) throws ConfigException {
		if (this.getConfig().contains(data.getName())) {
			this.getConfig().set(data.getName(), null);
		}
		String name = data.getName();
		this.getConfig().set(name + ".StartTitle", data.getStartTitle());
		this.getConfig().set(name + ".EndTitle", data.getEndTitle());
		this.getConfig().set(name + ".StartTime", data.getStartTime());
		this.getConfig().set(name + ".EndTime", data.getEndTime());
		this.getConfig().set(name + ".back", data.canback());
		for (WatchGroupTeleporter wgt : data.getTPS()) {
			this.setWatchGroupTeleporter(wgt, data.getName());
		}
		try {
			this.saveConfig();
		} catch (IOException e) {
			throw new ConfigException(e, "保存失败");
		}
	}

	private void setWatchGroupTeleporter(WatchGroupTeleporter wgt, String wgdName) {
		String path = wgdName + ".Teleporters." + wgt.getName();
		this.getConfig().set(path + ".Title", wgt.getTitle());
		this.getConfig().set(path + ".loc", wgt.getLocation());
		this.getConfig().set(path + ".WaitTime", wgt.getWaitTime());
		this.getConfig().set(path + ".Name", wgt.getName());

	}

	public boolean contains(String string) {
		return this.getConfig().contains(string);
	}

	public void remove(String name) throws ConfigException {
		this.getConfig().set(name, null);
		try {
			this.saveConfig();
		} catch (IOException e) {
			throw new ConfigException(e, "删除失败");
		}
	}
}
